#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
const int N = 5e5 + 10;
struct node {
	int ai;
	int bi;
	//int f;
}s[N];
int n, m;
bool  cmp(node A, node B) {
	return A.bi > B.bi;
}
int main() {
	int t;
	cin >> t;
	while (t--) {
		scanf_s("%d%d", &n, &m);
		for (int i = 0; i < n + 1; i++) {
			s[i].ai = 0;
			s[i].bi = 0;
		}
		int a1 = 0, b1 = 0;
		LL ans = 0;
		int cnt = 0;
		int idx = 0;
		for (int i = 1; i <= n; i++) {
			int a, b;
			scanf_s("%d%d", &a, &b);
			if (a >= b) {
				ans += a;
				a1 = a, b1 = b;
				idx++;
			}
			else {
				s[cnt++] = { a,b };
			}
		}
		if (idx == 1) {
			ans = ans - a1 + b1;
		}
		sort(s, s + cnt, cmp);
		if (idx == 0) {

		}
		int k = m - idx - cnt;
		for (int i = 0; i < cnt; i++) {
			if (i < k) {
				ans += s[i].bi;
			}
			else ans += s[i].ai;
		}
		printf("%lld\n", ans);
	}
	return 0;
}
